/* Author : Maisa Nurul Agnia */
package jtk.project4.fleet.screen.AddEditPurchaseOrder;

import java.awt.FlowLayout;
import java.util.List;


import jtk.project4.fleet.action.CancelPoAction;
import jtk.project4.fleet.action.SavePoAction;
import jtk.project4.fleet.domain.PoD;
import jtk.project4.fleet.event.AddEditPurchaseOrderEvent;
import jtk.project4.fleet.field.AddEditDeleteField;
import jtk.project4.fleet.field.DataBuyerField;
import jtk.project4.fleet.field.ExtendedCostField;
import jtk.project4.fleet.field.LocationField;
import jtk.project4.fleet.field.NotesField;
import jtk.project4.fleet.field.PartsToOrderField;
import jtk.project4.fleet.field.PoStatusField;
import jtk.project4.fleet.field.PurchaseOrderPartDownField;
import jtk.project4.fleet.field.PurchaseOrderPartUpField;
import jtk.project4.fleet.field.PurchasedFromVendorField;
import jtk.project4.fleet.field.ShipToField;
import jtk.project4.fleet.field.TotalsField;

import nl.coderight.jazz.action.Action;
import nl.coderight.jazz.form.FormField;
import nl.coderight.jazz.form.FormHeader;
import nl.coderight.jazz.form.FormLayout;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.FormLayout.Align;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.control.TabControl;
import nl.coderight.jazz.form.control.TabItem;
import nl.coderight.jazz.form.field.TableField;
import nl.coderight.jazz.form.field.TableField.AutoResizeMode;
import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.form.validation.ValidationError;

public class AddEditPurchaseOrderView extends FormView{
	private TabControl POTab;
	private TabItem purchaseOrderTab;
	private TabItem partsToOrderTab;
	private FormHeader formHeader;
	private PushButton submitButton;
	private PushButton cancelButton;
	private PushButton helpButton;
	private TableField<PoD>  partsToOrderTable;
	private Action cancelAction;
	private Action submitAction;
	private Action submitAction2;
	
	public void handleEvent(AddEditPurchaseOrderEvent evt)  
	{
		switch(evt.getType())
		{
			case CANCEL:
				close();
				break;
				
			case SAVE:
			//	System.out.print("view");
				
				// check for validation errors
				List<ValidationError> errors = validate();
				if (!errors.isEmpty()) {
					// there are validation errors so do not submit...
					errors.get(0).getField().focus();
					break;
				} else {
					// submit form and close view
			
					submit();					
					close();
					// fall-through!
				}
			default:
				propagateEvent(evt);
				break;
			
		}
		
	}
	protected void init() {
		// TODO Auto-generated method stub
		createTabFields();
		createAction();
		createFields();
		createLayout();
	}

	private void createAction() {
		// TODO Auto-generated method stub
		submitAction = new SavePoAction();
		submitAction2 = new SavePoAction(); // cara buat save+ di message box?
		cancelAction = new CancelPoAction();
		// enable submit button when form is changed
		setOnChangeAction(submitAction);
		
	}
	private void createTabFields()
	{
		POTab = new TabControl();
		purchaseOrderTab = new TabItem("Purchase Order");
		partsToOrderTab = new TabItem("Parts To Order");

	}
	
	private FormField createTabs()
	{
		TabControl form = POTab;
		form.addTab(purchaseOrderTab, createPurchaseOrderTab());
		form.addTab(partsToOrderTab, createPartsToOrderTab());
		return form;
	}
	
	private GroupControl createPurchaseOrderTab() {
		GroupControl form = new GroupControl();
		form.setLayout(new FormLayout())
		//.addField(new DataBuyerField("po"))
		.addField(new PurchaseOrderPartUpField("po"))
		.addRow()
		.addField(new PurchaseOrderPartDownField("po"))
		;		
		
		return form;
	}
	
	private GroupControl createPartsToOrderTab() {
		GroupControl form = new GroupControl();
		form.setLayout(new FormLayout())
		.addField(partsToOrderTable)
		.addRow()
		.addField(new ExtendedCostField("ExtendedCost"))
		.addRow()
		.addField(new AddEditDeleteField("AddEditDeleteButton"))
		;
		return form;
	}
	
	private void createFields()
	{
			partsToOrderTable = new TableField<PoD>("poDs","poD");
		//	partsToOrderTable.setTitle("partsToOrderTable.header");
			partsToOrderTable.setAutoResizeMode(AutoResizeMode.ALL_COLUMNS);
			partsToOrderTable.getSelectionValues();
			
			partsToOrderTable.addColumn("label.Quantity","qtyO");
			partsToOrderTable.addColumn("label.Part#","partId");
			partsToOrderTable.addColumn("label.Name","name");
			partsToOrderTable.addColumn("label.UnitCost","unitCost");
			partsToOrderTable.addColumn("label.ExtendedCost","eCost");
			partsToOrderTable.addColumn("label.ReorderQuantity","qtyR");//Table.addColumn("label.qty","qty");
			partsToOrderTable.addColumn("label.Comments","comments");
		
		submitButton = new PushButton(new SavePoAction());
		cancelButton = new PushButton(new CancelPoAction());
		helpButton = new PushButton("helpAction");
	}
	
	private void createLayout()
	{
		setHeader(createHeader());
		addField(createForm());
		
	}
	
	private FormHeader createHeader()
	{
		formHeader = new FormHeader();
		formHeader.setTitle("AddEditPurchaseOrder.header.form");
		return formHeader;
	}
	private GroupControl createForm()
	{
		GroupControl form = new GroupControl();
		form.addField(createTabs());
		form.addField(createButtons(),SOUTH);
		return form;
	}

	
	private GroupControl createButtons()
	{
		GroupControl buttons = new GroupControl();
		buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttons.addField(submitButton);
		buttons.addField(cancelButton);
		buttons.addField(helpButton);
		return buttons;
		
	}
	

}
